/*
 * ws_segment.cpp

 *
 *  Created on: Feb 28, 2013
 *      Author: think
 */
#include "ws_segment.h"
CRFPP::Tagger *tagger;
int ws_segment_load(char * path_model) {
	char path[512] = "";
	sprintf(path, "-m %s -v 3 -n2", path_model);
	//char *arg[] ={"CRF++","-m",path_model,"-v","3","-n","2"};
	tagger = CRFPP::createTagger(path);
}
char w[] = { -32, -71, -128, 44, -32, -71, -127, 44, -32, -71, -126, 44, -32,
		-71, -125, 44, -32, -71, -124, '\0' }; //"เ,แ,โ,ใ,ไ";
char v[] = { -32, -72, -71, 44, 32, -32, -72, -72, 44, 32, -32, -72, -73, 44,
		32, -32, -72, -74, 44, 32, -32, -72, -75, 44, 32, -32, -72, -76, 44, 32,
		-32, -72, -77, 44, 32, -32, -72, -78, 44, 32, -32, -72, -79, 44, 32,
		-32, -72, -80, 44, 32, -32, -71, -115, 44, 32, -32, -72, -90, 44, 32,
		-32, -72, -92, 44, 32, -32, -71, -123, 44, 32, -32, -71, -121, '\0' }; //"ู, ุ, ื, ึ, ี, ิ, ำ, า, ั, ะ, ํ, ฦ, ฤ, ๅ, ็";
char d[] = "3, 2, 1, 0, 7, 6, 5, 4, 9, 8";
char t[] = { -32, -71, -120, 44, 32, -32, -71, -119, 44, 32, -32, -71, -118, 44,
		32, -32, -71, -117, '\0' }; //"่, ้, ๊, ๋";
char s[] = { -32, -72, -81, 44, 32, -32, -71, -116, 44, 32, 46, 44, 32, -32,
		-71, -122, '\0' }; //"ฯ, ์, ., ๆ";
char c[] = { -32, -72, -84, 44, 32, -32, -72, -83, 44, 32, -32, -72, -86, 44,
		32, -32, -72, -88, 44, 32, -32, -72, -87, 44, 32, -32, -72, -89, 44, 32,
		-32, -72, -91, 44, 32, -32, -72, -94, 44, 32, -32, -72, -93, 44, 32,
		-32, -72, -96, 44, 32, -32, -72, -95, 44, 32, -32, -72, -97, 44, 32,
		-32, -72, -98, 44, 32, -32, -72, -103, 44, 32, -32, -72, -104, 44, 32,
		-32, -72, -101, 44, 32, -32, -72, -102, 44, 32, -32, -72, -107, 44, 32,
		-32, -72, -108, 44, 32, -32, -72, -105, 44, 32, -32, -72, -106, 44, 32,
		-32, -72, -111, 44, 32, -32, -72, -112, 44, 32, -32, -72, -109, 44, 32,
		-32, -72, -110, 44, 32, -32, -72, -115, 44, 32, -32, -72, -114, 44, 32,
		-32, -72, -113, 44, 32, -32, -72, -120, 44, 32, -32, -72, -118, 44, 32,
		-32, -72, -117, 44, 32, -32, -72, -124, 44, 32, -32, -72, -122, 44, 32,
		-32, -72, -121, 44, 32, -32, -72, -127, 44, 32, -32, -72, -126, 44, 32,
		-32, -72, -125, '\0' };
//"ฬ, อ, ส, ศ, ษ, ว, ล, ย, ร, ภ, ม, ฟ, พ, น, ธ, ป, บ, ต, ด, ท, ถ, ฑ, ฐ, ณ, ฒ, ญ, ฎ, ฏ, จ, ช, ซ, ค, ฆ, ง, ก, ข, ฃ";
char q[] = "\", \'";
char p[] = "_";
char n[] = { -32, -72, -82, 44, 32, -32, -72, -116, 44, 32, -32, -72, -99, 44,
		32, -32, -72, -100, 44, 32, -32, -72, -85, 44, 32, -32, -72, -119, 44,
		32, -32, -72, -123, '0' }; //"ฮ, ฌ, ฝ, ผ, ห, ฉ, ฅ";

int IndexOf(char *str1, char *str2) {
	char* pch = strstr(str1, str2);
	while (pch != NULL) {
		return pch - str1;
	}
	return -1;
}
int strlen_utf8(char *str) {
	int i = 0;
	int j = 0;
	while (str[i]) {
		if ((str[i] & 0xC0) != 0x80)
			j++;
		i++;
	}
	return (j);
}
void get_at_utf8(char *des, char *str, int index) {
	int i = 0, old_i = 0;
	int j = 0;
	char str2[4]="";
	while (str[i]) {
		if ((str[i] & 0xC0) != 0x80) {

			if (j == index + 1) {
				strncpy(str2, str + old_i, i - old_i);
				str2[i - old_i] = '\0';
				//printf("\nstr=%s old_i=%d , i=%d , j=%d", str2, old_i, i, j);

			}
			old_i = i;
			j++;
		}
		//printf("\n%d) %c", i, str[i]);
		i++;
	}
	if (j == index + 1) {
		strncpy(str2, str + old_i, i - old_i);
		str2[i - old_i] = '\0';
		//printf("\nstr=%s old_i=%d , i=%d , j=%d", str2, old_i, i, j);
	}
	if(strcmp(str2,"")==0)
		strcpy(des,"");
	else
		strcpy(des,str2);
}
char * wordseg_getradical(char *str) {

	if (IndexOf(w, str) != -1) {
		return "w\0";
	} else if (IndexOf(v, str) != -1) {
		return "v\0";
	} else if (IndexOf(d, str) != -1) {
		return "d\0";
	} else if (IndexOf(t, str) != -1) {
		return "t\0";
	} else if (IndexOf(s, str) != -1) {
		return "s\0";
	} else if (IndexOf(c, str) != -1) {
		return "c\0";
	} else if (IndexOf(q, str) != -1) {
		return "q\0";
	} else if (IndexOf(p, str) != -1) {
		return "p\0";
	} else if (IndexOf(n, str) != -1) {
		return "n\0";
	} else {
		return "o\n";
	}
}
char * ws_segment_tokken(char *text) {
	int textlen = strlen(text) + 1;
	char *string_buffer = (char *) calloc(textlen * 2, sizeof(char)); //don't free
	strcpy(string_buffer, "|");
	char **string_array = (char **) calloc(textlen, sizeof(char *)); //free
	char **spacebreak = (char **) calloc(textlen, sizeof(char *));
	;  //free
	char *linebreak = (char *) calloc(textlen, sizeof(char));

	strcpy(linebreak, text);
	if (strcmp(linebreak, "") != 0) {
		if (strlen(linebreak) == 0) {
			strcat(string_buffer, "\n");
		} else {
			int i = 0, x = 0; //count token
			char *tok = strtok(linebreak, " ");
			textlen = strlen(tok);
			spacebreak[i] = (char *) calloc(textlen + 1, sizeof(char));
			strcpy(spacebreak[i], tok);
			//printf("\nLine %d :%s",i,spacebreak[i]);
			while (1) {
				tok = strtok(NULL, " ");
				if (tok == NULL)
					break;
				i++;
				textlen = strlen(tok);
				spacebreak[i] = (char *) calloc(textlen + 1, sizeof(char));
				strcpy(spacebreak[i], tok);
				//printf("\nLine %d :%s",i,spacebreak[i]);
			}

			for (int j = 0; j <= i; j++) {
				//printf("\nline : %s",spacebreak[j]);
				//printf("\nlen : %d",strlen_utf8(spacebreak[j]));
				textlen = strlen(spacebreak[j]);

				char *word = (char *) calloc(textlen + 1, sizeof(char));
				strcpy(word, spacebreak[j]);
				char ans[10] = "";

				int wordlen = strlen_utf8(word);
				int k = 0;
				//printf("\nwoedlen=%d", wordlen);
				for (k = 0; k < wordlen; k++) {
					//printf("\nprint out = %s",word);
					if (k == 0 && strcmp(word, "_") != 0)
						strcpy(ans, "B");
					else if (k > 0)
						strcpy(ans, "I");
					else
						strcpy(ans, "");

					if (strcmp(word, "_") == 0)
						strcpy(ans, "B");

					char line[30] = "";
					//IndexOf("",word);
					char tmp[10] = "";
					get_at_utf8(tmp, word, k);
					int len = strlen(tmp);
					string_array[k] = (char *) calloc(len + 1, sizeof(char));
					strcpy(string_array[k], tmp);
					strcat(line, tmp);
					strcat(line, "\t");
					//printf("\n word %s",tmp);
					char *red = wordseg_getradical(tmp);
					strcat(line, red);
					strcat(line, "\t");
					strcat(line, ans);
					//printf("\n%s", line);

					tagger->add(line);

					//return "สวัสดีครับ";
				}
				free(word);
				if (!tagger->parse())
					return "";
				//String line = "";
				int m = 0;
				//printf("\nbuffer=%s",string_buffer);
				for (m = 0; m < (int) tagger->size(); m++) {
					//printf("\nbuffer=%s",string_buffer);

					if (strcmp(tagger->y2(m), "B") == 0 && x != 0) {
						strcat(string_buffer, "|");
						char tmp2[2000];
						//get_at_utf8(tmp2,string_array,m);
						strcpy(tmp2, string_array[m]);
						strcat(string_buffer, tmp2);
					} else {
						char tmp2[2000];
						strcpy(tmp2, string_array[m]);
						strcat(string_buffer, tmp2);
						x++;
					}

				}
				//printf("\nbuffer=%s",string_buffer);

				if (j < i)
					strcat(string_buffer, "| ");
				else
					strcat(string_buffer, "|");

				tagger->clear();
			}
		}
	}

	int i = 0;
	for (i = 0; i < textlen; i++) {
		free(string_array[i]);
		free(spacebreak[i]);
	}
	free(string_array);
	free(spacebreak);
	free(linebreak);

	return string_buffer;
}

